package com.wechar_rpa.service;

import com.wechatpay.java.service.payment.WeChatPayService;
import com.wechatpay.java.service.payment.model.UnifiedOrderRequest;
import com.wechatpay.java.service.payment.model.UnifiedOrderResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class WeChatPayService {

    private final com.wechatpay.java.service.payment.WeChatPayService weChatPayService;

    public WeChatPayService(com.wechatpay.java.service.payment.WeChatPayService weChatPayService) {
        this.weChatPayService = weChatPayService;
    }

    public UnifiedOrderResponse createPrepayOrder(String userId, int planLevel) {
        SubscriptionPlan plan = subscriptionService.getPlanByLevel(planLevel);
        if (plan == null) return null;

        UnifiedOrderRequest request = new UnifiedOrderRequest();
        request.setAppid("your-wechat-appid"); // 替换为你的微信 AppID
        request.setMchid("your-mch-id"); // 商户号
        request.setDescription("会员等级订阅 - Level " + planLevel);
        request.setOutTradeNo(UUID.randomUUID().toString().replace("-", "")); // 本地订单号
        request.setAmount(new UnifiedOrderRequest.Amount().setTotal(plan.getMonthlyPrice().intValue()));
        request.setNotifyUrl("https://yourdomain.com/api/wechatpay/notify"); // 支付回调地址
        request.setTradeType("JSAPI"); // JSAPI 支付方式

        return weChatPayService.unifiedOrder(request);
    }
}